package com.hzyc.internetshop.controller;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.hzyc.internetshop.bean.Fankui;
import com.hzyc.internetshop.bean.Goods;
import com.hzyc.internetshop.bean.Imgs;
import com.hzyc.internetshop.bean.Orders;
import com.hzyc.internetshop.bean.Page;
import com.hzyc.internetshop.bean.Types;
import com.hzyc.internetshop.bean.User;
import com.hzyc.internetshop.service.impl.HoutaiServiceImp;
import com.hzyc.internetshop.service.impl.TypeServiceImp;

@Controller
public class HoutaiController {
	@Autowired
	private HoutaiServiceImp hs;
	
	@Autowired
	private TypeServiceImp ts;
	
	@Autowired
	private Page page;
	
	//锟斤拷锟酵计�
	@RequestMapping("add_ad.do")
	public String add_ad(HttpServletResponse response,HttpServletRequest request,Goods good,MultipartFile ad_img1,
			MultipartFile ad_img2,MultipartFile ad_img3,MultipartFile ad_img4,MultipartFile ad_img5,MultipartFile ad_img6){
		
		
		try {
			
			Imgs img=new Imgs();
			
			String ad_img1_name = ad_img1.getOriginalFilename();
			String finalPath1="D:/2锟斤拷锟斤拷锟酵计�"+ad_img1_name;
			File file1 = new File(finalPath1);
			ad_img1.transferTo(file1);		
			img.setImg_1(ad_img1_name);
			
			String ad_img2_name = ad_img2.getOriginalFilename();
			String finalPath2="D:/2锟斤拷锟斤拷锟酵计�"+ad_img2_name;
			File file2 = new File(finalPath2);
			ad_img2.transferTo(file2);		
			img.setImg_2(ad_img2_name);
			
			String ad_img3_name = ad_img3.getOriginalFilename();
			String finalPath3="D:/2锟斤拷锟斤拷锟酵计�"+ad_img3_name;
			File file3 = new File(finalPath3);
			ad_img3.transferTo(file3);		
			img.setImg_3(ad_img3_name);
			
			String ad_img4_name = ad_img4.getOriginalFilename();
			String finalPath4="D:/2锟斤拷锟斤拷锟酵计�"+ad_img4_name;
			File file4= new File(finalPath4);
			ad_img4.transferTo(file4);		
			img.setImg_4(ad_img4_name);
			
			String ad_img5_name = ad_img5.getOriginalFilename();
			String finalPath5="D:/2锟斤拷锟斤拷锟酵计�"+ad_img5_name;
			File file5 = new File(finalPath5);
			ad_img5.transferTo(file5);		
			img.setImg_5(ad_img5_name);
			
			String ad_img6_name = ad_img6.getOriginalFilename();
			String finalPath6="D:/2锟斤拷锟斤拷锟酵计�"+ad_img6_name;
			File file6 = new File(finalPath6);
			ad_img6.transferTo(file6);		
			img.setImg_6(ad_img6_name);
			
			
			
			boolean bol=hs.add_ad(img);
			if(bol==true){
				return "redirect:show_ads.do";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}

//	锟斤拷询锟矫伙拷	
	@RequestMapping("/show_user.do")
	public void show_user(HttpSession session,HttpServletResponse response,String username,String password){
	
		boolean bol=false;
		try {
		
			response.setCharacterEncoding("utf-8");
			PrintWriter out=response.getWriter();
			
			
			String result=hs.getPassword(username);
			if(result==null||!result.equals(password)){
				
				bol=false;
				
			}else if(result.equals(password)){
				bol=true;
				session.setAttribute("username1", username);
				
			}
			out.print(bol);
			out.flush();
			out.close();
		
		
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("tuichu.do")
	public String tuichu(HttpSession session){
		session.setAttribute("username1", "");
		return "dengru.jsp";
	}
	
	//注锟斤拷
	@RequestMapping("/zhucefocus.do")
	public void zhucefocus(HttpServletResponse response,String username){
		
		boolean bol=true;
		try {
			
		
			List<User> list=hs.showuser();
			
			for(int i=0;i<list.size();i++){
				if(list.get(i).getUsername().equals(username)){
					bol=false;
				
				}
			}
			
				response.setCharacterEncoding("utf-8");
				PrintWriter out=response.getWriter();
				out.print(bol);
				out.flush();
				out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	@RequestMapping("zhuce.do")
	public String zhuce(HttpServletResponse response,String username,String password){
		boolean bol=true;
		try {
			List<User> list=hs.showuser();
			for(int i=0;i<list.size();i++){
				if(list.get(i).getUsername().equals(username)){
					bol=false;
				}
			}
			if(bol==true){
				User u=new User();
				u.setPassword(password);
				u.setUsername(username);
				hs.addUser(u);
			}
			response.setCharacterEncoding("utf-8");
			PrintWriter out=response.getWriter();
			out.print(bol);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	//锟斤拷品锟斤拷锟斤拷
	@RequestMapping("getdetail.do")
	public ModelAndView getdetail(String id){
		ModelAndView mav=new ModelAndView();
		try {
			Goods g=ts.getGoodById(id);
			List<Types> list= ts.showType();
			mav.addObject("typeList", list);
			mav.addObject("good", g);
			mav.setViewName("detail.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}

	//锟斤拷示锟斤拷锟�
	@RequestMapping("/show_ads.do")
	public ModelAndView show_ads(){
		ModelAndView mav=new ModelAndView();
		List<Imgs> list=hs.show_ads();
		mav.addObject("list", list);
		mav.setViewName("show_ad.jsp");
		return mav;
	}
	
	//删锟斤拷锟斤拷
	@RequestMapping("delete_ad.do")
	public String delete_ad(String id){
		hs.delete_ad(id);
		return "redirect:show_ads.do";
		
	}

	//锟斤拷询锟斤拷锟�
	@RequestMapping("look_ad.do")
	public ModelAndView lood_ad(String id){
		ModelAndView mav=new ModelAndView();
		Imgs img=hs.look_ad(id);
		
		
		mav.addObject("list", img);
	
		mav.setViewName("look_ad.jsp");
		return mav;
	}
	
	@RequestMapping("start_ad.do")
	public String start_ad(String id){
		
		
		hs.remove_ad();
		hs.use_ad(id);
		return "redirect:show_ads.do";
		
	}
	
	
	//锟斤拷询锟斤拷锟斤拷
	@RequestMapping("select_orders.do")
	public ModelAndView select_orders(String name,HttpSession session,HttpServletResponse response,HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		try {
			if("".equals(name) || name==null){
				session.setAttribute("name", "");
			}else{
				session.setAttribute("name", name);
			}
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			page.startposition();
			page.setName(name);
		
			page.setMaxpage(hs.getordernum()%page.getPagesize()==0?ts.getgoodnum()%page.getPagesize():(hs.getordernum()/page.getPagesize()+1));
			List<Orders> list=hs.select_orders(page);
			
			
			mav.addObject("oList", list);
			mav.addObject("page", page);
			mav.setViewName("select_orders.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return mav;
	}
	
	
//	锟斤拷一页
	@RequestMapping("/pageUp2.do")
	public String pageUp(int nowPage,HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.uppage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "select_orders.do?name="+name;
	}
//	锟斤拷一页	
	@RequestMapping("/pageDown2.do")
	public String pageDown(int nowPage,HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.downpage(nowPage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "select_orders.do?name="+name;
	}
//	锟斤拷页
	@RequestMapping("/pageFirst2.do")
	public String pageFirst(HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
			
			page.firstpage();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "select_orders.do?name="+name;
	}
//	尾页
	@RequestMapping("/pageLast2.do")
	public String pageLast(HttpSession session){
		String name=session.getAttribute("name").toString();
		try {
		
			page.lastpage();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "select_orders.do?name="+name;
	}
	
	//统锟斤拷
	@RequestMapping("go_tongji.do")
	public ModelAndView go_tongji(){
		ModelAndView mav=new ModelAndView();
		try {
			List<String> year_price = new ArrayList<String>();
			
			for(int i=1;i<=9;i++){
				List<String> list=hs.tongji_allprice("2016-0"+i);
				double sum = 0;
				for(int j=0;j<list.size();j++){
					sum+=Double.parseDouble(list.get(j));	
				}
				
				year_price.add(sum+"");
			}
			for(int i=0;i<=2;i++){
				List<String> list=hs.tongji_allprice("2016-1"+i);
				double sum = 0;
				for(int j=0;j<list.size();j++){
					sum+=Double.parseDouble(list.get(j));	
				}
				year_price.add(sum+"");
			}
			mav.addObject("price", year_price);
			mav.setViewName("tongji.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	//选锟斤拷锟斤拷锟�
	@RequestMapping("change_year.do")
	public ModelAndView change_year(String year){
		ModelAndView mav=new ModelAndView();
		try {
			List<String> year_price = new ArrayList<String>();
			
			for(int i=1;i<=9;i++){
				List<String> list=hs.tongji_allprice(year+"-0"+i);
				double sum = 0;
				for(int j=0;j<list.size();j++){
					sum+=Double.parseDouble(list.get(j));	
				}
				
				year_price.add(sum+"");
			}
			for(int i=0;i<=2;i++){
				List<String> list=hs.tongji_allprice(year+"-0"+i);
				double sum = 0;
				for(int j=0;j<list.size();j++){
					sum+=Double.parseDouble(list.get(j));	
				}
				year_price.add(sum+"");
			}
			mav.addObject("year", year);
			mav.addObject("price", year_price);
			mav.setViewName("tongji2.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	//锟叫伙拷锟矫伙拷
	@RequestMapping("qiehuanUser.do")
	public String qiehuanUser(HttpSession session){
		try {
			
			session.removeAttribute("username1");
			return "redirect:dengru.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/*//锟斤拷锟斤拷锟斤拷锟斤拷
		@RequestMapping("getdetail1.do")
		public ModelAndView getdetail1(String id){
			ModelAndView mav=new ModelAndView();
			try {
				Article a=ts.getArticleById(id);
				List<Types> list= ts.showType();
				mav.addObject("typeList", list);
				mav.addObject("good", g);
				mav.setViewName("detail.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return mav;
		}*/
	@RequestMapping("delete_orders.do")
	public String delete_good(String id){
	
		try {
			
			boolean bol=ts.deleteorder(id);
			if(bol==true){
				return "redirect:select_orders.do";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	//浼氬憳鏌ヨ绠＄悊
		@RequestMapping("vip_manager.do")
		public ModelAndView select_vip(String name,HttpSession session,HttpServletResponse response,HttpServletRequest request){
			ModelAndView mav=new ModelAndView();
			try {
				if("".equals(name) || name==null){
					session.setAttribute("name", "");
				}else{
					session.setAttribute("name", name);
				}
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				page.startposition();
				page.setName(name);
			
				page.setMaxpage(ts.getusernum()%page.getPagesize()==0?ts.getusernum()%page.getPagesize():(ts.getusernum()/page.getPagesize()+1));
				List<User> list=ts.selectvip(page);
				
				mav.addObject("uList", list);
				mav.addObject("page", page);
				mav.setViewName("select_vip.jsp");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return mav;
		}
//		锟斤拷一页
		@RequestMapping("/pageUp22.do")
		public String pageUp22(int nowPage,HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
				
				page.uppage(nowPage);
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "select_orders.do?name="+name;
		}
//		锟斤拷一页	
		@RequestMapping("/pageDown22.do")
		public String pageDown22(int nowPage,HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
				
				page.downpage(nowPage);
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "select_orders.do?name="+name;
		}
//		锟斤拷页
		@RequestMapping("/pageFirst22.do")
		public String pageFirst22(HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
				
				page.firstpage();
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "select_orders.do?name="+name;
		}
//		尾页
		@RequestMapping("/pageLast22.do")
		public String pageLast22(HttpSession session){
			String name=session.getAttribute("name").toString();
			try {
			
				page.lastpage();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return "select_orders.do?name="+name;
		}
		@RequestMapping("getFankui.do")
		public ModelAndView getFankui(String id){
			ModelAndView mav=new ModelAndView();
			try {
				List<Fankui> list=ts.getFankuiById(id);
				mav.addObject("fankuiList", list);
				mav.setViewName("fankui.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return mav;
		}
}



